/**@FILE:WishServiceImpl.java
 * @AUTHOR:qingsheng.zheng
 * @DATE:2015-1-31 下午6:31:06*/
package com.xf.toy.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.xf.pub.exception.ServiceException;
import com.xf.pub.tool.StringTool;
import com.xf.toy.bean.Wish;
import com.xf.toy.bean.WishExample;
import com.xf.toy.bean.WishExample.Criteria;
import com.xf.toy.dao.WishMapper;
import com.xf.toy.dao.page.Pagination;
import com.xf.toy.service.OrderService;
import com.xf.toy.service.WishService;
import com.xf.toy.service.constant.ToyConst;

/*******************************************
 * @COMPANY:alibaba
 * @CLASS:WishServiceImpl
 * @DESCRIPTION:
 * @AUTHOR:qingsheng.zheng
 * @VERSION:v1.0
 * @DATE:2015-1-31 下午6:31:06
 *******************************************/
@Service("wishService")
public class WishServiceImpl implements WishService {
	@Resource
	private WishMapper wishMapper;
	@Resource
	private OrderService orderService;

	@Override
	public void userWish(Long userid, String toycode) {
		Wish wish = new Wish();
		wish.setCreatetime(new Date());
		wish.setStatus(ToyConst.TOY_WISH_STATUS_WAIT);
		wish.setToyCode(toycode);
		wish.setUserId(userid);
        this.validateWish(userid, ToyConst.TOY_WISH_STATUS_WAIT+","+ToyConst.TOY_WISH_STATUS_ORDER);
		wishMapper.insert(wish);

	}
	public void  validateWish(Long userid,String status)
	{
		Wish wish = new Wish();
		wish.setUserId(userid);
		wish.setStatus(status);
		int count = this.selectCountByWish(wish);
		if (count > 0)
			throw new ServiceException(10012L);
	}
	
	

	public WishExample buildExample(Wish wish) {
		WishExample wishExample = new WishExample();
		Criteria criteria = wishExample.createCriteria();
		if (wish.getUserId() != null)
			criteria.andUserIdEqualTo(wish.getUserId());
		if (!StringUtils.isBlank(wish.getStatus())) {
			String[] statuss = wish.getStatus().split(",");
			List<String> statusList = StringTool.toArray(statuss);
			criteria.andStatusIn(statusList);
		}
		return wishExample;
	}

	public int selectCountByWish(Wish wish) {
		return wishMapper.countByExample(buildExample(wish));

	}

	public WishMapper getWishMapper() {
		return wishMapper;
	}

	public void setWishMapper(WishMapper wishMapper) {
		this.wishMapper = wishMapper;
	}

	@Override
	public List<Wish> selectUserWishs(Long userid, List<String> status,
			Pagination page) {
		WishExample wishExample = new WishExample();
		Criteria criteria = wishExample.createCriteria();
		if (userid != null) {
			criteria.andUserIdEqualTo(userid);
		}
		if (page != null) {
			wishExample.setLimitStart(page.getStart());
			wishExample.setLimitEnd(page.getPageSize());
		}
		criteria.andStatusIn(status);
		Integer count = wishMapper.countByExample(wishExample);
		page.setTotalcount(count);
		return wishMapper.selectByExample(wishExample);
	}

	@Override
	public Wish selectWishByWishId(Long wishId) {
		return wishMapper.selectByPrimaryKey(wishId);

	}

	@Override
	public void updateWish(Wish wish) {
		WishExample example = new WishExample();
		Criteria criteria = example.createCriteria();
		if (wish.getStatus() != null) {
			criteria.andStatusEqualTo(wish.getStatus());
		}
		wishMapper.updateByPrimaryKeySelective(wish);
	}

}
